package top.jacktgq.concurrent_utils.pool;

import java.util.concurrent.SynchronousQueue;

import top.jacktgq.utils.LogUtils;
import top.jacktgq.utils.Sleeper;

/**
 * 
 * @Title: SynchronousQueueDemo.java 
 * @Package top.jacktgq.concurrent_utils.pool 
 * @Description: 测试使用SynchronousQueue，它一般配合CachedThreadPool使用
 * @author CandyWall   
 * @date 2020年11月4日 下午12:56:16 
 * @version V1.0
 */
public class SynchronousQueueDemo {
	public static void main(String[] args) {
		SynchronousQueue<Integer> integerQueue = new SynchronousQueue<Integer>();
		new Thread(() -> {
			try {
				LogUtils.log("SynchronousQueueDemo", "putting 1");
				integerQueue.put(1);
				LogUtils.log("SynchronousQueueDemo", "putted 1");
				LogUtils.log("SynchronousQueueDemo", "putting 2");
				integerQueue.put(2);
				LogUtils.log("SynchronousQueueDemo", "putted 2");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t1").start();;
		
		Sleeper.sleep(1);
		
		new Thread(() -> {
			LogUtils.log("SynchronousQueueDemo", "take 1");
			try {
				integerQueue.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2").start();
		
		Sleeper.sleep(1);
		
		new Thread(() -> {
			LogUtils.log("SynchronousQueueDemo", "take 2");
			try {
				integerQueue.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t3").start();
		
	}
}
